package com.micro.aop.registrar;

import com.micro.aop.interceptor.LoggerMethodInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;

public class AopPluginBeanRegistrar extends SimpleBeanDefinitionRegistry {

    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition) throws BeanDefinitionStoreException {
        AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
        advisor.setExpression("");
        advisor.setAdvice(new LoggerMethodInterceptor());
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition();
        GenericBeanDefinition genericBeanDefinition = (GenericBeanDefinition) builder.getBeanDefinition();
        genericBeanDefinition.setBeanClass(AspectJExpressionPointcutAdvisor.class);
        genericBeanDefinition.setInstanceSupplier(() -> advisor);
        super.registerBeanDefinition(beanName, genericBeanDefinition);
    }
}
